package com.mrobuy.jdvc.service.impl;

import com.gavel.database.SQLExecutor;
import com.gavel.utils.StringUtils;
import com.mrobuy.jdvc.dao.*;
import com.mrobuy.jdvc.entity.*;
import com.mrobuy.jdvc.service.ItemPropService;
import com.mrobuy.util.IdentifierGen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mrobuy.util.CodeEncoder;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service("itemPropService")
public class ItemPropServiceImpl implements ItemPropService {

    private static Logger LOG = LoggerFactory.getLogger(ItemPropService.class);

    @Autowired
    private VCCatePropDao vcCatePropDao;

    @Autowired
    private VCCateExtPropDao vcCateExtPropDao;

    @Autowired
    private VCItemPropDao vcItemPropDao;

    @Autowired
    private VCItemExtPropDao vcItemExtPropDao;

    @Autowired
    private VCPropValueDao vcPropValueDao;


    @Autowired
    private VCExtPropValueDao vcExtPropValueDao;

    @Override
    public List<VCItemPropValue> loadPropValues(VCProduct product, VCItem item) {

        if ( product==null || StringUtils.isBlank(product.getCid1())) {
            return Collections.EMPTY_LIST;
        }

        List<VCCategoryProp> categoryProps = vcCatePropDao.load(product.getCid1());
        if ( categoryProps==null || categoryProps.size()==0 ) {
            System.out.println("[Cate: " + product.getCid1() + "]规格属性为空");
            return Collections.EMPTY_LIST;
        }


        Map<String, VCItemPropValue> map = new ConcurrentHashMap<>();
        List<VCItemPropValue> propValues = vcItemPropDao.load(product.getId());
        if ( (propValues==null || propValues.size() == 0)&& item!=null ) {
            propValues = vcItemPropDao.load("MYMRO-"+CodeEncoder.decoder(item.getItemNum()));
        }
        if ( propValues!=null && propValues.size()>0 ) {
            for (VCItemPropValue propValue : propValues) {
                map.put(String.valueOf(propValue.getPropid()), propValue);
            }
        }

        List<VCItemPropValue> _propValues = new ArrayList<>();

        for (VCCategoryProp categoryProp : categoryProps) {
            VCItemPropValue itemPropValue = null;
            if ( map.containsKey(categoryProp.getPropid()) ) {
                itemPropValue = map.get(categoryProp.getPropid());
            } else {
                itemPropValue = new VCItemPropValue();
                itemPropValue.setPropid(Integer.parseInt(categoryProp.getPropid()));
                itemPropValue.setPropname(categoryProp.getPropname());
                itemPropValue.setType(categoryProp.getInputtype());
                itemPropValue.setRequired(categoryProp.getRequired());
            }

            itemPropValue.setOptions(vcPropValueDao.load(categoryProp.getPropid()));

            _propValues.add(itemPropValue);
        }

        return _propValues;
    }


    /**
     * 更新规格属性值
     * @param product
     * @param data
     */
    @Override
    public void updtaeProps(VCProduct product, Map<String, String> data) {


        List<VCCategoryProp> cateProps = vcCatePropDao.load(product.getCid1());
        if ( cateProps==null ) {
            System.out.println("[Cate: " + product.getCid1() + "]规格属性为空");
            return;
        }

        if ( data==null || data.size()==0 ) {
            System.out.println("规格属性数据为空");
            return;
        }

        Map<String, VCItemPropValue> map = new ConcurrentHashMap<>();
        List<VCItemPropValue> propValues = vcItemPropDao.load(product.getId());
        if ( propValues!=null ) {
            for (VCItemPropValue propValue : propValues) {
                map.put(String.valueOf(propValue.getPropid()), propValue);
            }
        }

        for (VCCategoryProp cateProp : cateProps) {
            VCItemPropValue propValue = null;
            if ( map.containsKey(cateProp.getPropid()) ) {
                propValue = map.get(cateProp.getPropid());
            }

            if ( propValue==null ) {
                propValue = new VCItemPropValue();
                propValue.setId(IdentifierGen.uuid());
                propValue.setProductid(product.getId());
                propValue.setPropid(Integer.parseInt(cateProp.getPropid()));
                propValue.setPropname(cateProp.getPropname());
                propValue.setType(cateProp.getInputtype());
                propValue.setRequired(cateProp.getRequired());
                vcItemPropDao.insert(propValue);
            }

            if ( data.containsKey(cateProp.getPropid()) && data.get(cateProp.getPropid())!=null  ) {
                String value = data.get(cateProp.getPropid());
                if ( "1".equalsIgnoreCase(cateProp.getInputtype()) ) {
                    propValue.setPropvid(value);
                }  else if ( "2".equalsIgnoreCase(cateProp.getInputtype()) ){
                    propValue.setPropvalues(value);
                }  else {
                    propValue.setPropremark(value);
                }
                vcItemPropDao.update(propValue);
            }

        }


    }

    @Override
    public List<VCItemExtPropValue> loadExtPropValues(VCProduct product, VCItem item) {

        if ( product==null || StringUtils.isBlank(product.getCid1())) {
            return Collections.EMPTY_LIST;
        }

        List<VCCategoryExtProp> cateExtProps = vcCateExtPropDao.load(product.getCid1());
        if ( cateExtProps==null || cateExtProps.size()==0 ) {
            System.out.println("[Cate: " + product.getCid1() + "]扩展属性为空");
            return Collections.EMPTY_LIST;
        }


        Map<String, VCItemExtPropValue> map = new ConcurrentHashMap<>();
        List<VCItemExtPropValue> extPropValues = vcItemExtPropDao.load(product.getId());
        if ( (extPropValues==null || extPropValues.size() == 0)&& item!=null ) {
            extPropValues = vcItemExtPropDao.load("MYMRO-"+CodeEncoder.decoder(item.getItemNum()));
        }
        if ( extPropValues!=null && extPropValues.size()>0 ) {
            for (VCItemExtPropValue extPropValue : extPropValues) {
                map.put(String.valueOf(extPropValue.getPropid()), extPropValue);
            }
        }


        List<VCItemExtPropValue> _propValues = new ArrayList<>();

        for (VCCategoryExtProp categoryProp : cateExtProps) {
            VCItemExtPropValue itemPropValue = null;
            if ( map.containsKey(categoryProp.getAttId()) ) {
                System.out.println("find:" +  map.get(categoryProp.getAttId())+ "]扩展属性为空");
                itemPropValue = map.get(categoryProp.getAttId());
            } else {
                System.out.println("not find:");
                itemPropValue = new VCItemExtPropValue();
                itemPropValue.setPropid(Integer.parseInt(categoryProp.getAttId()));
                itemPropValue.setPropname(categoryProp.getName());
                itemPropValue.setType(categoryProp.getInputType());
                itemPropValue.setRequired(categoryProp.getIsRequired());
            }

            itemPropValue.setOptions(vcExtPropValueDao.load(categoryProp.getAttId()));

            _propValues.add(itemPropValue);
        }

        return _propValues;
    }

    @Override
    public void updtaeExtProps(VCProduct product, Map<String, String> data) {

        List<VCCategoryExtProp> cateExtProps = vcCateExtPropDao.load(product.getCid1());
        if ( cateExtProps==null ) {
            System.out.println("[Cate: " + product.getCid1() + "]扩展属性为空");
            return;
        }

        if ( data==null || data.size()==0 ) {
            System.out.println("扩展属性数据为空");
            return;
        }

        Map<Integer, VCItemExtPropValue> map = new ConcurrentHashMap<>();
        List<VCItemExtPropValue> extPropValues = vcItemExtPropDao.load(product.getId());
        if ( extPropValues!=null ) {
            for (VCItemExtPropValue extPropValue : extPropValues) {
                map.put(extPropValue.getPropid(), extPropValue);
            }
        }

        for (VCCategoryExtProp cateExtProp : cateExtProps) {
            VCItemExtPropValue propValue = null;
            Integer attrId =  Integer.parseInt(cateExtProp.getAttId());
            if ( map.containsKey(attrId) ) {
                propValue = map.get(attrId);
            }

            if ( propValue==null ) {
                propValue = new VCItemExtPropValue();
                propValue.setId(IdentifierGen.uuid());
                propValue.setProductid(product.getId());
                propValue.setPropid(attrId);
                propValue.setPropname(cateExtProp.getName());
                propValue.setType(cateExtProp.getInputType());
                propValue.setRequired(cateExtProp.getIsRequired());
                vcItemExtPropDao.insert(propValue);
            }

            if ( data.containsKey(cateExtProp.getAttId()) && data.get(cateExtProp.getAttId())!=null  ) {
                String value = data.get(cateExtProp.getAttId());
                if ( "1".equalsIgnoreCase(cateExtProp.getInputType()) || "2".equalsIgnoreCase(cateExtProp.getInputType())) {
                    propValue.setExtvalue(value);
                } else {
                    propValue.setExtremark(value);
                }
                vcItemExtPropDao.update(propValue);
            }

        }

    }
}
